package cn.col.gszf.controller.root;

import cn.col.gszf.model.RestResult;
import cn.col.gszf.model.UserVoteGroupDay;
import cn.col.gszf.pojo.UserVotes;
import cn.col.gszf.service.UserVotesService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description
 * @Author zhaoning
 * @Date 2024/6/25
 */
@RestController
public class VotesDataController {
    @Resource
    private UserVotesService userVotesService;


    @GetMapping("/result/today")
    public RestResult<HashMap<String, Integer>> getTodayVoting(){
        HashMap<String, Integer> result = userVotesService.getTodayResult();
        return RestResult.success(200, result);
    }

    @GetMapping("/result/trend/")
    public RestResult getRecentNums(){
        HashMap<String, Long> mp = new HashMap<>();
        List<UserVoteGroupDay> userVoteGroupDays = userVotesService.calByDay();
        Date curDate = new Date();
        //初始化近一个月所有的时间
        for(int i = 0; i < 30; i++){
            curDate = DateUtil.offset(curDate, DateField.DAY_OF_MONTH, -1);
            mp.put(DateUtil.format(curDate, "yyyy/MM/dd"), 0L);
        }
        for(UserVoteGroupDay groupDay : userVoteGroupDays){
            mp.put(DateUtil.format(groupDay.getTime(), "yyyy/MM/dd"), groupDay.getNums());
        }
        return RestResult.success(200, JSONUtil.toJsonStr(mp));
    }

    @GetMapping("/result/trend/{channelId}")
    public RestResult getRecentNumsByChannelId(@PathVariable Integer channelId){
        HashMap<String, Long> mp = new HashMap<>();
        List<UserVoteGroupDay> userVoteGroupDays = userVotesService.calByDayAndChannelId(channelId);
        Date curDate = new Date();
        //初始化近一个月所有的时间
        for(int i = 0; i < 30; i++){
            curDate = DateUtil.offset(curDate, DateField.DAY_OF_MONTH, -1);
            mp.put(DateUtil.format(curDate, "yyyy/MM/dd"), 0L);
        }
        for(UserVoteGroupDay groupDay : userVoteGroupDays){
            mp.put(DateUtil.format(groupDay.getTime(), "yyyy/MM/dd"), groupDay.getNums());
        }
        return RestResult.success(200, JSONUtil.toJsonStr(mp));
    }


    @GetMapping("/result/record")
    public RestResult<List<UserVotes>> get(){
        Sort.Direction sort = Sort.Direction.DESC;
        Pageable pageable = PageRequest.of(0, 50, sort, "createAt");
        List<UserVotes> ret = userVotesService.findAllUserVotes((Page<UserVotes>) pageable);
        return RestResult.success(200, ret);
    }

}
